import Vue from 'vue'
import App from './App'

//加载系统配置文件
const appconfig = require('@/appconfig.js');
const applog = require('@/static/core/fwlog.js');
const apptool = require('@/static/core/fwtool.js');
const appauth = require('@/static/core/fwauthority.js');
const appmd5 = require('@/static/core/fwmd5.js');
const appsign = require('@/static/core/fwsign.js');
const appwechat = require('@/static/core/fwwechat.js');
// import appconfig from '@/appconfig.js';
// import applog from '@/static/core/fwlog.js';
// import apptool from '@/static/core/fwtool.js';
// import appauth from '@/static/core/fwauthority.js';
// import appmd5 from '@/static/core/fwmd5.js';
// import appsign from '@/static/core/fwsign.js';
// import appwechat from '@/static/core/fwwechat.js';

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	...App,
	created() {
		this.appconfig = appconfig;
		this.applog = applog;
		this.apptool = apptool;
		this.appauth = appauth;
		this.appmd5 = appmd5;
		this.appsign = appsign;
		this.appwechat = appwechat;
	},
	methods: {
		//基础请求
		_post(api, params, method, successcallback, failcallback, completecallback) {
			//==============公用参数 开始====================
			//(固定参数)
			params.fwplatform = this.appconfig.fwplatform;
			params.fwdevice = this.appconfig.fwdevice;
			params.fwversion = this.appconfig.fwversion;
			//(用户身份参数)
			let authInfo = this.appauth.getAuthInfo();
			if (authInfo) {
				// params.userId = authInfo.userId;
				// params.userToken = authInfo.userToken;
				params.user_id = authInfo.userId;
				params.user_token = authInfo.userToken;
			}
			//==============公用参数 结束====================
			//组建API地址
			let requestAPI = this.appconfig.fwrootapi + api;
			//请求签名
			params.fwrndstr = appsign.createRndStr(32);
			params.fwrndnum = appsign.createRndNum(32);
			params.fwsign = appsign.makeSign(params, this.appconfig.keysecret);

			apptool.requestApi(requestAPI, params, method, successcallback, function() {
				apptool.showModalInfo('网络异常！');
				failcallback && failcallback();
			}, completecallback);
		},

		// 检测登录
		_checkLogin(refresh) {
			let loginStatus = this.appauth.checkAuth();
			if (!loginStatus) {
				apptool.showToastInfo('请登录后再操作！', null, 1000, function(res) {
					//如果需要回调，则必须在当前面实现refreshPage()方法
					uni.navigateTo({
						url: '/pages/basic/login/index' + (refresh ? '?refreshpage=refresh' : ''),
					});
				});
			}
			return loginStatus;
		},

		// 模拟帐号
		_virtualLogin() {
			if ((!appconfig.release) && (appconfig.testData.testUserId > 0)) {
				//身份数据缓存到本地
				appauth.setUserCacheData({
					user_id: appconfig.testData.testUserId,
					user_token: appconfig.testData.testUserToken,
					nickname: '模拟帐号',
					avatar: '/pages/res/owner/icon_owner_portrait.png',
					mobile: '15512345678'
				});
			}
		}
	}
})
app.$mount()
